﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AzureProvider.ssdsClient;

namespace AzureProvider.SoupMethods
{
    public class UserRole
    {
        public string Id;
        public string UserID;
        public string RoleID;
    }


    public class UserRolesMethods : BaseMethods 
    {
        public static string Kind = "UserRole";

        protected override string containerId
        {
            get { return "UserContainer"; }
        }

        protected override string kind
        {
            get { return Kind; }
        }

        protected override Entity CreateEntity(object Object)
        {
            UserRole myobj = (UserRole)Object;

            var entity = new Entity()
            {
                Id = myobj.Id,
                Kind = kind
            };

            entity.Properties = new Dictionary<string, object>();
            entity.Properties.Add("UserID", myobj.UserID);
            entity.Properties.Add("RoleID", myobj.RoleID);

            return entity;
        }

        public override object CreateObject(Entity Entity)
        {
            UserRole myObj = new UserRole();
            myObj.Id = Entity.Id;
            myObj.UserID = (string)Entity.Properties.Single(tt => tt.Key == "UserID").Value;
            myObj.RoleID = (string)Entity.Properties.Single(tt => tt.Key == "RoleID").Value;

            return myObj;
        }

        public UserRole GetUserRole(string UserID, string RoleID)
        {
            List<Entity> ents = GetEntities(UserID, RoleID);
            if (ents.Count > 0)
            {
                return (UserRole)CreateObject(ents.First());
            }
            else
            {
                return null;
            }
        } 
        protected virtual List<Entity> GetEntities(string UserID, string RoleID)
        {
            string errMessage;
            var qry = "from e in entities where e.Kind == \"" + kind + "\" && e[\"UserID\"]==\"" + UserID + "\" && e[\"RoleID\"]==\"" + RoleID + "\"  select e";
            List<Entity> ents = GetEntities(qry, out errMessage);
            return ents;
        }

        public void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            RolesMethods RolesMethods = new RolesMethods();
            MembershipUserMethods MembershipUserMethods = new MembershipUserMethods();

            string errMessage ;
            foreach (string username in usernames)
            {
                string userid = MembershipUserMethods.GetMembershipUser(username).Id;
                foreach (string rolename in roleNames)
                { 
                    string roleid = RolesMethods.GetRole(rolename,out errMessage).Id;

                    Insert(
                        new UserRole
                        {
                            Id = Guid.NewGuid().ToString(),
                            RoleID = roleid,
                            UserID = userid
                        }
                    , out errMessage);
                }
            }
        }

        public  void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            RolesMethods RolesMethods = new RolesMethods();
            MembershipUserMethods MembershipUserMethods = new MembershipUserMethods();

            string errMessage;
            foreach (string username in usernames)
            {
                string userid = MembershipUserMethods.GetMembershipUser(username).Id;
                foreach (string rolename in roleNames)
                {
                    string roleid = RolesMethods.GetRole(rolename, out errMessage).Id;

                    Delete (GetUserRole(userid,roleid).Id );
                }
            }
        }

       
    }
}
